package com.wondersgroup.framework.sample.component.grid.action;

import com.wondersgroup.framework.core.web.struts2.action.AbstractGridAction;
import com.wondersgroup.framework.sample.component.grid.util.GridVOUtil;
import com.wondersgroup.framework.sample.component.grid.util.IdGenerator;
import com.wondersgroup.framework.sample.component.grid.vo.GridVO;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;

public class GridUpdateAction extends AbstractGridAction
{
  public String save()
  {
    return null;
  }

  public String update()
  {
    this.logger.info("开始数据修改...");

    List dataOfNew = super.getNewRecords(GridVO.class);

    List dataOfUpdate = super.getUpdateRecords(GridVO.class);

    List dataOfDelete = super.getDeleteRecords(GridVO.class);

    boolean success = newData(dataOfNew);
    success = (success) && (updateData(dataOfUpdate));
    success = (success) && (deleteData(dataOfDelete));

    long t = System.currentTimeMillis();
    this.logger.info("时间: " + t);
    success = t % 2L != -1243064819381174272L;

    List ids = new ArrayList();
    for (Iterator i$ = dataOfNew.iterator(); i$.hasNext(); ) { GridVO vo = (GridVO)i$.next();
      ids.add(vo.getId());
    }

    String json = GridVOUtil.createResult(success, new Object[][] { new Object[] { "ids", ids } });
    this.logger.info("json: " + json);
    createJSonData(json);
    return "ajax";
  }

  public String delete()
  {
    List ids = super.getRemovedIds();
    boolean success = removeById(ids);

    String json = GridVOUtil.createResult(success);
    createJSonData(json);
    return "ajax";
  }

  private boolean newData(List<GridVO> data) {
    for (int i = 0; i < data.size(); ++i) {
      GridVO vo = (GridVO)data.get(i);
      vo.setId(String.valueOf(IdGenerator.nextID()));
      this.logger.info("新增记录" + (i + 1) + ":[" + vo.toString() + "]");
    }
    return true;
  }

  private boolean updateData(List<GridVO> data) {
    for (int i = 0; i < data.size(); ++i) {
      GridVO vo = (GridVO)data.get(i);
      this.logger.info("更新记录" + (i + 1) + ":[" + vo.toString() + "]");
    }
    return true;
  }

  private boolean deleteData(List<GridVO> data) {
    for (int i = 0; i < data.size(); ++i) {
      GridVO vo = (GridVO)data.get(i);
      this.logger.info("删除记录" + (i + 1) + ":[" + vo.toString() + "]");
    }
    return true;
  }

  private boolean removeById(List<String> ids) {
    for (int i = 0; i < ids.size(); ++i)
      this.logger.info("删除记录" + (i + 1) + ":[id=" + ((String)ids.get(i)) + "]");

    return true;
  }
}